//
//  MapPOIPlugin.m
//  Runner
//
//  Created by mac on 2022/4/26.
//

#import "MapPOIPlugin.h"
#import "MapPOIViewController.h"

@interface MapPOIPlugin()<MapPOIViewDelgate>

@property (nonatomic, strong) UIViewController *controller;
@property(nonatomic, copy) FlutterResult result;
@end

@implementation MapPOIPlugin

static NSString *const CHANNELNAME = @"com.ahj/livePlayPlugin";
static NSString *const METHODNAME  = @"livePlay";

/**
 *plugin 注册
 */
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
    FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:CHANNELNAME binaryMessenger:[registrar messenger]];
    
    UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;

    MapPOIPlugin *plugin = [[MapPOIPlugin alloc]initWithViewController:viewController];
    
    [registrar addMethodCallDelegate:plugin channel:channel];
}

/**
 *flutter端通过通道调用原生方法时会进入以下回调
 */
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result{
    
    if ([call.method isEqualToString:METHODNAME]) {
        self.result = result;
        NSDictionary *dic = call.arguments;
        
        MapPOIViewController *mapVC = [[MapPOIViewController alloc] init];
        mapVC.address = dic[@"address"];
        mapVC.delegate = self;
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mapVC];
        nav.modalPresentationStyle = 0;
        [self.controller presentViewController:nav animated:YES completion:nil];
    } else {
        result(FlutterMethodNotImplemented);
    }
}

- (void)mapPOIViewDelgate:(NSDictionary *)poiData{
    self.result(poiData);
}


- (instancetype)initWithViewController:(UIViewController *)viewController {
    self = [super init];
    if (self) {
        self.controller = viewController;
    }
    return self;
}
@end
